package com.jxbeidou.system.org.controller;

import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.system.org.convert.PostConvert;
import com.jxbeidou.system.org.entity.PostDO;
import com.jxbeidou.system.org.service.PostService;
import com.jxbeidou.system.org.query.PostQuery;
import com.jxbeidou.system.org.vo.PostVO;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* 岗位管理
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/post")
@AllArgsConstructor
public class PostController {
    @Resource
    private PostService postService;

    @PostMapping("/page")
    public R<PageResult<PostVO>> page(@RequestBody @Valid PostQuery query){
        PageResult<PostVO> page = postService.page(query);

        return R.success(page);
    }

    @GetMapping("/{id}")
    public R<PostVO> get(@PathVariable("id") Long id){
        PostDO entity = postService.getById(id);

        return R.success(PostConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public R<String> save(@RequestBody PostVO vo){
        postService.save(vo);

        return R.saveSuccess();
    }

    @PutMapping
    public R<String> update(@RequestBody @Valid PostVO vo){
        postService.update(vo);

        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id){
        postService.delete(Arrays.asList(id));

        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    public R<String> delete(@RequestBody List<Long> idList){
        postService.delete(idList);

        return R.deleteSuccess();
    }
}